package cn.edu.uestc.smgt.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.uestc.smgt.common.Response;
import cn.edu.uestc.smgt.common.StatusType;
import cn.edu.uestc.smgt.pojo.Buzhu;
import cn.edu.uestc.smgt.pojo.dto.AssistQuery;
import cn.edu.uestc.smgt.pojo.dto.BuZhuDTO;
import cn.edu.uestc.smgt.service.BuZhuService;
@Controller
@RequestMapping("/usr/")
public class BuZhuController {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private BuZhuService buzhuService;

	@RequestMapping(value = "/buzhu/add", method = RequestMethod.POST) 
	@ResponseBody
	public Object add(HttpServletRequest request,@RequestBody Buzhu buzhu) {
		try{
			int status = buzhuService.add(buzhu);
			String message = StatusType.value(status).getMessage();
			return new Response(status,message);
		}catch(Exception e){
			logger.error("调用BuZhuController.add出错,Buzhu={},error={}",new Object[]{buzhu,e});
			return new Response(StatusType.EXCEPTION.getVal(),StatusType.EXCEPTION.getMessage());
		}
	}
	@RequestMapping(value = "/buzhu/remove", method = RequestMethod.POST)
	@ResponseBody
	public Object remove(HttpServletRequest request,@RequestParam int id) {
		try{
			int status = buzhuService.remove(id);
			String message = StatusType.value(status).getMessage();
			return new Response(status,message);
		}catch(Exception e){
			logger.error("调用BuZhuController.remove出错,id={},error={}",new Object[]{id,e});
			return new Response(StatusType.EXCEPTION.getVal(),StatusType.EXCEPTION.getMessage());
		}
	}
	
	@RequestMapping(value = "/buzhu/submit", method = RequestMethod.POST)
	@ResponseBody
	public Object submit(HttpServletRequest request,@RequestParam String buzhuList) {
		try{
			int status = buzhuService.batchUpdate(buzhuList);
			String message = StatusType.value(status).getMessage();
			return new Response(status,message);
		}catch(Exception e){
			logger.error("调用BuZhuController.submit出错,buzhuList={},error={}",new Object[]{buzhuList,e});
			return new Response(StatusType.EXCEPTION.getVal(),StatusType.EXCEPTION.getMessage());
		}
	}
	@RequestMapping(value = "/buzhu/getBuzhuByPage", method = RequestMethod.GET)
	@ResponseBody
	public Object getBuzhuByPage(HttpServletRequest request,
			             @RequestParam int startPage,@RequestParam int pageNum,@RequestParam int statusCode) {
		try{
			List<BuZhuDTO> buzhuList = buzhuService.getBuzhuByPage(startPage, pageNum, statusCode);
			return new Response(StatusType.SUCCESS.getVal(),StatusType.SUCCESS.getMessage(),buzhuList);
		}catch(Exception e){
			logger.error("调用BuZhuController.getBuzhuByPage出错,error={}",new Object[]{e});
			return new Response(StatusType.EXCEPTION.getVal(),StatusType.EXCEPTION.getMessage());
		}
	}
}
